package com.study_online.controller;

import com.study_online.domain.ResultInfo;
import com.study_online.utils.JjwtUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.study_online.domain.User;
import com.study_online.service.Impl.UserServiceImpl;

import com.study_online.utils.MailUtils;
import com.study_online.utils.TimeUtils;
import lombok.SneakyThrows;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;



/**
 * @author 孙远帅
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userServiceImpl;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @RequestMapping("/test")
    @ResponseBody
    public ResultInfo test() {
        ResultInfo info = userServiceImpl.selectAll("user ");
        return info;
    }

    /**
     * 用户登录
     *
     * @return
     */
    @SneakyThrows
    @RequestMapping(value = "/login")
    @ResponseBody
    public ResultInfo login(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        ResultInfo info = userServiceImpl.login(username, password);
        User user1 = (User) info.getData();
        if (user1 != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("username", user1.getUsername());
            map.put("password", user1.getPassword());
            String token = JjwtUtil.createJWT(String.valueOf(user1.getUid()), map);
            //存活2个小时
            redisTemplate.opsForValue().set(user1.getUid() + "", token, 2, TimeUnit.HOURS);
            info.setData(token);
        }
        return info;
    }

    @PostMapping("/get")
    @ResponseBody
    public ResultInfo getEFlyReturnPR(@RequestParam Map<String, String> params) {
        System.out.println("接收到的x-www-form-urlencoded");
        System.out.println(params);
        // ResultInfo info = userServiceImpl.selectAll();
        return null;
    }

    @SneakyThrows
    @RequestMapping(value = "/logining")
    @ResponseBody
    public ResultInfo logining(String account, String password) {
        ResultInfo info = userServiceImpl.logining(account, password);
        User user = (User) info.getData();
        Map<String, Object> map = new HashMap<>();
        return info;
    }

    /**
     * 注册用户
     * @param user
     * @param request
     * @return
     */
    @RequestMapping(value = "/register")
    @ResponseBody
    public ResultInfo register(@RequestBody User user, HttpServletRequest request) {
        user.setAddTime(TimeUtils.getTime());
        String code = request.getParameter("code");
        String email = user.getEmail();
        String username = user.getUsername();
        ResultInfo info = new ResultInfo();
        if (!code.equalsIgnoreCase((String) redisTemplate.opsForValue().get(email))) {
            info.setFlag(false);
            info.setErrorInfo("验证码错误！");
            return info;
        }
        //判断邮箱是否注册
        ResultInfo resu1 = userServiceImpl.findEmail(email);
        if (resu1.isFlag()) {
            System.out.println(resu1.isFlag());
            info.setFlag(false);
            info.setErrorInfo("邮箱已经注册");
            return info;
        }
        //判断用户名是否存在
        ResultInfo info1 = userServiceImpl.findUsername(username);
        if (info1.isFlag()) {
            System.out.println(info1.isFlag());
            info.setFlag(false);
            info.setErrorInfo("用户名存在,重新输入注册");
            return info;
        }

        return userServiceImpl.register(user, code);
    }

    /**
     * 判断用户名是否存在
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "/findUsername")
    @ResponseBody
    public ResultInfo findUsername(@RequestBody String username) {
        ResultInfo info=null;
        if (username != null) {
            username = username.substring(13, username.length() - 2);
            info = userServiceImpl.findUsername(username);
        }
        return info;
    }

    /**
     * 发送邮件
     *
     * @param email
     * @return
     */
    @RequestMapping("/sendEmail")
    @ResponseBody
    public ResultInfo sendEmail(@RequestBody String email) {
        System.out.println(email);
        ResultInfo info = new ResultInfo();
        if (email != null) {
            email = email.substring(10, email.length() - 2);
            //判断邮箱是否注册
            ResultInfo resu1 = userServiceImpl.findEmail(email);
            if (resu1.isFlag()) {
                System.out.println(resu1.isFlag());
                info.setFlag(false);
                info.setErrorInfo("邮箱已经注册");
                return info;
            }

            String code = MailUtils.sendMail(email);
            redisTemplate.opsForValue().set(email, code, 2, TimeUnit.HOURS);
            info.setFlag(true);
        }
        return info;
    }

    /**
     * 首页视频展示
     *
     * @return
     */
    @RequestMapping("/adminVideo")
    @ResponseBody
    public ResultInfo adminVideo(@RequestParam("videoIds") List<Integer> videoIds) {
        System.out.println(videoIds);
        ResultInfo info = userServiceImpl.selectVideoList(videoIds);
        return info;
    }


    @RequestMapping("/selectHomeVideo")
    @ResponseBody
    public ResultInfo selectHomeVideo() {
        ResultInfo info = userServiceImpl.selectHomeVideo();
        return info;
    }

    /**
     * 首页公告展示
     *
     * @return
     */
    @RequestMapping("/adminNotice")
    @ResponseBody
    public ResultInfo adminNotice(List<Integer> noticeIds) {
        ResultInfo info = userServiceImpl.selectNoticeList(noticeIds);
        return info;
    }

}